import axios from 'axios'
import store from "../store";
import { getToken,setToken, removeToken } from './cookie'
import {Message} from "element-ui";
const consoleURL = process.env.VUE_APP_7_OPEN

let baseURL = '';

if (process.env.NODE_ENV === 'development') {
  baseURL = '/index.php';
  //baseURL = '/mytest';
  //baseURL = 'http://192.168.44.150/api/app/obor-nginx-php/';
}
else{
  baseURL = process.env.VUE_APP_BASE_API_BPF
}
console.log('建平台域名：'+baseURL)

const service_bpf = axios.create({
  baseURL,
  timeout: 100000, // 请求超时时间
  headers: {
    'X-Server-Name': 'tyun'
  }
})
// request拦截,在请求发起前搞事情
service_bpf.interceptors.request.use((config) => {
  config.headers['X-Server-Name'] ='tyun';
  if (process.env.NODE_ENV === 'development') {
    // setToken('Zyf9xiKHKBgYEn4J4HB73tdDLNlAh4MQ')
	// setToken('LuxT9YGrAgLOvsKIwzkbkbxmhtZz2Yo9')
  }

  if (getToken()) { // 测试环境
    config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  }
  return config
})

// respone拦截,获取请求数据后，搞事情
service_bpf.interceptors.response.use(
    // 正常获取数据

    response => {
      if (response.data && response.data.code === 200) {
        return response.data
      } else {
        if (response.data.code === 401) {
          removeToken('X-Token')
          window.location.href = consoleURL + 'login/login?backUrl=' + escape(window.location.href)
          return Promise.reject(response.data)
        } else if (response.data.code === 403) {
          // 抱歉，你无权访问该页面
          // router.push('/403')
          console.log(response)
          window.location.href = consoleURL + '403'
        } else {
          let url = response.config.url
          let apiURL = url.substring(baseURL.length)
          if(apiURL.indexOf('/') === 0){
            apiURL = apiURL.substring(1)
          }
          // console.log('url',url)
          // console.log('apiURL',apiURL)
          // console.log('response',response.data)
          // 当请求的接口是【中文站-任务列表】不需要弹框
          if(apiURL === ('tweb/site/taskList') || apiURL === ('tweb/statistic/getDataBydate') || apiURL === ('tweb/enSite/getManageUrl') || apiURL === ('tweb/site/getDesktopUrl') || (apiURL === ('tweb/statistic/pagetopn') && response.data.message === '该用户尚未开通流量统计！') || (apiURL === ('tweb/Statistic/getCount') && response.data.message === '该用户尚未开通流量统计！') || (apiURL === ('tweb/statistic/getCountryData') && response.data.message === '该用户尚未开通流量统计！') || (apiURL === ('tweb/site/statisticsInfo') && response.data.message === '2.0网站接口调用失败！')) {
            return Promise.reject(response.data)
          }
          let message = response.data.message ? response.data.message : response.data.data.msg ? response.data.data.msg: response.data.data.data.msg ? response.data.data.data.msg: '接口异常'
          Message({type: 'error', message: message})
          return Promise.reject(response.data)
        }
      }
    },
    // 请求失败
    error => {
      console.log('请求错误',error)
      return Promise.reject('error', error)
    },
)

export default service_bpf
